﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Function;
using BLL.Permission;

namespace UI.Permission
{
    /// <summary>
    /// 权限方法
    /// </summary>
    public class RightCheck
    {
        /// <summary>
        /// 多种检查的页面
        /// </summary>
        public static List<permissionType> pendingCheckList { get; set; }

        /// <summary>
        /// 检查是否拥有某种权限
        /// </summary>
        /// <param name="type">权限类型</param>
        /// <param name="ActByTrue">如果为true,则在拥有某项权限时执行某项动作 为false则没有权限时执行</param>
        /// <param name="action">拥有或没有权限时(取决于参数ActByTrue 默认没有权限时执行)执行的行为执行的行为 为空则不执行</param>
        /// <returns></returns>
        public static Boolean HavePermission(permissionType type, Boolean ActByTrue = false, Action action = null)
        {
            var result = (CheckPermission(type) == 100);
            //执行action
            if (ActByTrue == result && action != null)
            {
                action();
            }
            return result;
        }

        /// <summary>
        /// 检查是否拥有某种自定义权限
        /// </summary>
        /// <param name="rightTag">权限标签</param>
        /// <param name="ActByTrue">如果为true,则在拥有某项权限时执行某项动作 为false则没有权限时执行</param>
        /// <param name="action">拥有或没有权限时(取决于参数ActByTrue 默认没有权限时执行)执行的行为执行的行为 为空则不执行</param>
        /// <returns></returns>
        public static bool HavePermission(string rightTag, Boolean ActByTrue = false, Action action = null)
        {
            //当前用户id
            var uid = Method.CurrentUserID;
            var result = false;
            if (new UserRoleAssignBLL().IsAdmin(uid))
                result = true;
            else
                result = new ExtendedRoleAuthorityBLL().HavePermission(rightTag, uid);
            //执行action
            if (ActByTrue == result && action != null)
            {
                action();
            }
            return result;
        }

        /// <summary>
        /// 检查是否拥有某种权限
        /// </summary>
        /// <param name="rightId">权限id</param>
        /// <param name="ActByTrue">如果为true,则在拥有某项权限时执行某项动作 为false则没有权限时执行</param>
        /// <param name="action">拥有或没有权限时(取决于参数ActByTrue 默认没有权限时执行)执行的行为执行的行为 为空则不执行</param>
        /// <returns></returns>
        public static Boolean HavePermission(int rightId, Boolean ActByTrue = false, Action action = null)
        {
            //当前用户id
            var uid = Method.CurrentUserID;
            var result = false;
            if (new UserRoleAssignBLL().IsAdmin(uid))
                result = true;
            else
                result = new ExtendedRoleAuthorityBLL().HavePermission(rightId, uid);
            //执行action
            if (ActByTrue == result && action != null)
            {
                action();
            }
            return result;
        }

        /// <summary>
        /// 检查是否拥有哪些自定义权限
        /// </summary>
        /// <param name="rights">在idlist中待检查拥有的权限id列表</param>
        /// <param name="Idlist">检查的权限id列表</param>
        /// <returns></returns>
        public static Boolean HavePermission(List<string> rights, params string[] Idlist)
        {
            return true;
        }

        /// <summary>
        /// 检查是否拥有哪些自定义权限
        /// </summary>
        /// <param name="rights">在idlist中待检查拥有的权限id列表</param>
        /// <param name="Idlist">检查的权限id列表</param>
        /// <returns></returns>
        public static Boolean HavePermission(string[] rights, params string[] Idlist)
        {
            return true;
        }

        /// <summary>
        /// 检查是否拥有哪些自定义权限
        /// </summary>
        /// <param name="Idlist">权限id列表</param>
        /// <returns></returns>
        public static Boolean HaveAllPermission(params string[] Idlist)
        {
            return true;
        }

        /// <summary>
        /// 检查是否拥有哪些自定义权限 返回拥有权限的id列表
        /// </summary>
        /// <param name="Idlist">权限id列表</param>
        /// <returns></returns>
        public static List<string> GetList(params string[] Idlist)
        {
            return new List<string>();
        }

        /// <summary>
        /// 权限类型
        /// </summary>
        public enum permissionType
        {
            /// <summary>
            /// 查看列表
            /// </summary>
            List = 0,

            /// <summary>
            /// 新增
            /// </summary>
            Add = 1,

            /// <summary>
            /// 编辑
            /// </summary>
            Edit = 2,

            /// <summary>
            /// 删除
            /// </summary>
            Delete = 3,

            /// <summary>
            /// 查询
            /// </summary>
            Query = 4,

            /// <summary>
            /// 详细
            /// </summary>
            View = 5
        }

        /// <summary>
        /// 检查权限 100 成功 错误信息: 0 mid为空 1 用户信息session丢失 2 无此权限
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public static int CheckPermission(permissionType permission)
        {
            var keyword = "";
            switch (permission)
            {
                case permissionType.List:
                    keyword = "List";
                    break;
                case permissionType.Add:
                    keyword = "Add";
                    break;
                case permissionType.Edit:
                    keyword = "Edit";
                    break;
                case permissionType.Delete:
                    keyword = "Del";
                    break;
                case permissionType.Query:
                    keyword = "Sel";
                    break;
                case permissionType.View:
                    keyword = "View";
                    break;
            }
            var uid = HttpContext.Current.User.Identity.Name.Split('|')[1].ToInt32();
            if (new UserRoleAssignBLL().IsAdmin(uid))
                return 100;
            var bll = new RoleAuthorityBLL();
            var mid = HttpContext.Current.Request.QueryString["mid"] ?? "";
            if (mid.IsEmpty())
                return 0;
            if (bll.ExistsByUser(uid, mid.ToInt32(), keyword))
                return 100;
            else
                return 2;
        }
    }
}